<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/*读取需要发送email daily的user列表*/
$sql = "SELECT * "." FROM " . $GLOBALS['ecs']->table('users') . " WHERE email_daily=1 ";
$client = $db->getAll($sql);

/*读取需要发送的email daily内容*/
$date = time(); //获取系统当前时间的Unix 时间戳，time()获取的时间戳比系统时间慢8小时，所以要加上
$date = $date - 3600*24;//在当前系统时间戳上减1天
$formatdate = date("Y-m-d", $date);//格式化日期时间的显示格式

$sql = "SELECT * "." FROM " . $GLOBALS['ecs']->table('projects').' AS p JOIN ' . $GLOBALS['ecs']->table('users') . ' AS u JOIN '.$GLOBALS['ecs']->table('budget') . ' AS b JOIN '.$GLOBALS['ecs']->table('trainertype').' AS t JOIN ' .$GLOBALS['ecs']->table('trainingtype').' AS tt JOIN ' .$GLOBALS['ecs']->table('sessionstype').' AS s JOIN ' .$GLOBALS['ecs']->table('sessionlength').' AS l ' ." WHERE createdDate like '%".$formatdate."%' AND p.uid=u.user_id AND p.budget=b.num AND p.trainerType=t.num AND p.trainingType=tt.num AND p.sessions=s.num AND p.length=l.num ";

$daily_msg = $db->getAll($sql);


/* 设置重置邮件模板所需要的内容信息 */
$template    = get_mail_template('email_daily');
$project_content = "\t\r";
if($daily_msg != '')
{
	for($i=0; $i<count($daily_msg); $i++)
	{
		/*
		$project_content = $project_content. "<a href=\"". $GLOBALS['ecs']->url() ."projects.php?act=view_project&pid=". $msg['pid']."\">";
		$project_content = $project_content. $msg['subject']." —— ".$msg['trainerType']." —— ".$msg['trainingType']." —— ". $msg['budgetType'];
		$project_content = $project_content. "</a><br/>";
		*/
		$project_content = $project_content.($i+1)." : " . $daily_msg[$i]['subject']." —— ".$daily_msg[$i]['trainerType']." —— ".$daily_msg[$i]['trainingType']." —— ". $daily_msg[$i]['budgetType']."—";
		$project_content = $project_content. $GLOBALS['ecs']->url() ."projects.php?act=view_project&pid=". $daily_msg[$i]['pid'] ;
		$project_content = $project_content. "\t\r";
	}
}

	    
$GLOBALS['smarty']->assign('user_name',   $user_name);
$GLOBALS['smarty']->assign('project_content', $project_content);
$GLOBALS['smarty']->assign('shop_name',   $GLOBALS['_CFG']['shop_name']);
$GLOBALS['smarty']->assign('send_date',   date('Y-m-d'));
$GLOBALS['smarty']->assign('sent_date',   date('Y-m-d'));
			
$content = $GLOBALS['smarty']->fetch('str:' . $template['template_content']);

foreach($client as $cli)
{			
	/* 发送确认重置密码的确认邮件 */
	$user_name = $cli['user_name'];
	
	$email = $cli['email'];

	send_mail($user_name, $email, $template['template_subject'], $content, $template['is_html']);
	
}
?>